import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_jpf_core/util/colors_util.dart';

extension TextFieldExtension on TextField {

  static TextField customer({TextEditingController controller,TextAlign textAlign = TextAlign.left,int maxLines = 1,
    List<TextInputFormatter> inputFormatters,InputDecoration decoration,ValueChanged<String> onChanged, TextStyle textStyle, bool obscureText = false,bool enable,hintStyle,suffixIconConstraints,prefixIconConstraints,TextInputType keyboardType,String hintText,Widget rightView,InputBorder enabledBorder,InputBorder disabledBorder, prefixIcon}) {

    return TextField(
        controller: controller,
        keyboardType: keyboardType,
        textAlign: textAlign,
        inputFormatters: inputFormatters,
        enabled: enable,
        style: textStyle,
        onChanged: (val){
          int maxLength = -1;
          inputFormatters.forEach((element) {
            if(element is LengthLimitingTextInputFormatter){
              LengthLimitingTextInputFormatter l = element;
              maxLength = l.maxLength;
            }
          }) ;
          onChanged(maxLength>-1 && val.length>maxLength? val.substring(0,maxLength):val);
        },
        obscureText:obscureText,
        maxLines: maxLines ,
        toolbarOptions: controller == null ? ToolbarOptions():  ToolbarOptions(copy: controller.text.length>0,),
        decoration: decoration ?? new InputDecoration(
            hintText: hintText,
            hintStyle: hintStyle ?? TextStyle(color: ColorsUtil.grey_999,fontSize: 14),
            enabledBorder:  enabledBorder ?? UnderlineInputBorder(
                borderSide: BorderSide(color: ColorsUtil.grey_line)),
            disabledBorder:disabledBorder?? UnderlineInputBorder(
                borderSide: BorderSide(color: ColorsUtil.grey_line)),
            suffixIcon: rightView,
            suffixIconConstraints: suffixIconConstraints,
            prefixIcon: prefixIcon,
            prefixIconConstraints: prefixIconConstraints
        ));
  }

}